const { waitTime } = require("../../common/common");
const common = require("../../common/common");

var ywrs={
    cars:null,
    addCarPoi:null,
    centerPoi:null,
    curImg:null,
};

function main(options){
    auto.waitFor()
    device.keepScreenOn()
    requestScreenCapture();
    setScreenMetrics(1080, 1920);
    let app_name = "亿万人生";
    // options=options||{};
    common.resetConsole();
    // common.init(options);
    ywrs.init();

}
ywrs.init =function(){
    
    var x = device.width/2
    var y = device.height-100
    
    ywrs.addCarPoi={x:x,y:y}
    ywrs.centerPoi={x:x,y:device.height/2}
    console.log("center ---:%d",device.height/2)
    let cars = ywrs.getCarPoi(ywrs.centerPoi.x,ywrs.centerPoi.y)
    ywrs.cars = cars;
    threads.start(function(){

        while (true){
            sleep(common.random(300,400))
            console.log("11111")
            ywrs.clickCar()
        }
    })
    
    while(true){
        log("脚本主线程");
        sleep(1000000)
    }
}



ywrs.getCarPoi = function(x,y){

    let cy= y+50

    let cars = [
        [260,cy-400 ],[520,cy-400],[800,cy-400 ],
        [260,cy-200 ],[520,cy-200],[800,cy-200 ],
        [260,cy     ],[520,cy    ],[800,cy     ],
        [260,cy+200 ],[520,cy+200],[800,cy+200  ],
        [260,cy+400 ],[520,cy+400],[800,cy+400  ],
    ]
    return cars
}


ywrs.clickCar=function(){
    // let cars = [
    //     [257,664 ],[523,664], [804,664],
    //     [297,780],[533,780], [803,784],
    //     [274,1004],[537,984],[814,992],
    //     [272,1173],[536,1181],[804,1184],
    //     [250,1370],[495,1384],[778,1385],
    // ]
    let cars = ywrs.cars
    for (let i = 0; i < cars.length; i++) {
        
        if (ywrs.isMain()){
            click(cars[i][0],cars[i][1])
            sleep(common.random(200,300))
        }
    }

    
    if(ywrs.isMain()){
        // 加车
        sleep(1000)
        while(ywrs.isMain() && ywrs.hasEmpty()){
            click(ywrs.addCarPoi.x,ywrs.addCarPoi.y)
            sleep(common.random(100,200))
        }
    }


    if (!ywrs.isMain()){
        // ywrs.isGG()
        sleep(200)
        let curImg = captureScreen();
        ywrs.curImg = curImg;
        ywrs.findClose("close1.jpg") || ywrs.findClose("close2.jpg") ||ywrs.findClose("close3.png") || 
        ywrs.findClose("close4.jpg") || ywrs.findClose("close5.jpg") ||
        
        ywrs.findGift("hb1.jpg") || ywrs.findGift("hb2.jpg") || ywrs.findGift("hb3.png") ||
        ywrs.findGift("hb4.jpg") || ywrs.findGift("hb5.jpg") || ywrs.findGift("hb6.jpg") || 
        ywrs.findGift("hb7.jpg") || ywrs.findGift("hb8.jpg") || ywrs.findGift("hb9.jpg")
        
    }
}

// 是否有空位
ywrs.hasEmpty = function(){
    var wx = images.load("http://auth.bogerj.cn/autojs/img/empty.jpg");

    var p = findImage(captureScreen(), wx, {
        threshold: 0.7 
    });

    if(p){
        return true
    }else{
        return false
    }
}


ywrs.isMain=function(){


    var wx = images.load("http://auth.bogerj.cn/autojs/img/main.jpg");
    var p = findImage(captureScreen(), wx, {
        threshold: 0.8,
    });
    var curActive = currentActivity();
    if(p){
        // console.log("游戏界面");
        
        console.log("main active : ",curActive)
        // console.log(curActive == "com.afollestad.materialdialogs.MaterialDialog")

        return true
    }else{
        // console.log("other active : ",curActive)
        // console.log("其他界面");
        return false
    }
}


ywrs.findGift=function(imgPath){
    if (!ywrs.curImg) {
        return false
    }
    var wx = images.load("http://auth.bogerj.cn/autojs/img/"+imgPath);
    var p = findImage(ywrs.curImg, wx, {
        threshold: 0.8
    });

    if(p){
        click(p.x+50,p.y+20 );
        console.log(imgPath+" gift1.....");
        return true
    }else{
        // console.log("其他界面");
        return false
    }
}


ywrs.findClose=function(img){

    if (!ywrs.curImg) {
        return false
    }

    var wx = images.load("http://auth.bogerj.cn/autojs/img/"+img);
    var p = findImage(ywrs.curImg, wx, {
        threshold: 0.7
    });

    if(p){
        click(p.x+20,p.y+20 )
        console.log(img +" close.....");
        return true
    }else{
        // console.log("其他界面");
        return false
    }
}


// 页广告
ywrs.isMainGG=function(){
    console.log("gg");
    try {
        let gg = id("qy_count_down_btn").exists();
        if (gg){
            console.log("广告。。。")
        }else{
            console.log(" not 广告。。。");
            return false;
        }
        
        let count =0
        while(true){
            let close = id("qy_trueview_close_img").findOne();
            if (close){
                sleep(100);
                close.click();
                break;
            }
            if (count>50){
                break;
            }
            console.log("is gg and no close ....");
            count++;
        }
        
    } catch (error) {
        console.log(error);
    }
}

// 广告业
ywrs.isGG=function(){

    try {
        let gg = id("qy_count_down_btn").exists();
        if (gg){
            console.log("广告。。。")
        }else{
            console.log(" not 广告。。。");
            return false;
        }
        
        let count =0
        while(true){
            let close = id("qy_trueview_close_img").findOne();
            if (close){
                sleep(500);
                close.click();
                break;
            }
            if (count>50){
                break;
            }
            console.log("is gg and no close ....");
            count++;
        }
    } catch (error) {
        console.log(error);
    }
}




main();

